// pages/index/house/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    filter:[],
    type: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中…',
    })
    const arr ={new:1,ershou:2,zufang:3, zhongjie:4};
    const title = { new: '新房', ershou: '二手房', zufang: '租房', zhongjie: '中介' };
    const filter = {
      new: [
        {
          title: '地区', is_show: false, list: [
            {
              value: '全部', is_selected: true
            },
            {
              value: '庆云县', is_selected: false, sub_list: [
                { value: '全部', is_selected: true },
                { value: '渤海路街道', is_selected: false },
                { value: '庆云镇', is_selected: false },
                { value: '常家镇', is_selected: false },
                { value: '尚堂镇', is_selected: false },
                { value: '崔口镇', is_selected: false },
                { value: '东辛店镇', is_selected: false },
                { value: '严务乡', is_selected: false },
                { value: '中丁乡', is_selected: false },
                { value: '徐园子乡', is_selected: false }]
            }
          ]
        },
        {
          title: '价格', is_show: false, list: [
            { value: '全部', is_selected: true },
            { value: '2000以下' },
            { value: '2000-5000' },
            { value: '5000-8000' },
            { value: '8000-10000' },
            { value: '10000-13000' },
            { value: '13000-20000' },
            { value: '20000以上' },
          ]
        },
      ],
      ershou: [
        {
          title: '地区', is_show: false, list: [
            {
              value: '全部', is_selected: true
            },
            {
              value: '庆云县', is_selected: false, sub_list: [
                { value: '全部', is_selected: true },
                { value: '渤海路街道', is_selected: false },
                { value: '庆云镇', is_selected: false },
                { value: '常家镇', is_selected: false },
                { value: '尚堂镇', is_selected: false },
                { value: '崔口镇', is_selected: false },
                { value: '东辛店镇', is_selected: false },
                { value: '严务乡', is_selected: false },
                { value: '中丁乡', is_selected: false },
                { value: '徐园子乡', is_selected: false }]
            }
          ]
        },
        {
          title: '价格', is_show: false, list: [
            { value: '全部', is_selected: true },
            { value: '20万以下' },
            { value: '20-30万' },
            { value: '30-50万' },
            { value: '50-80万' },
            { value: '80-100万' },
            { value: '100-120万' },
            { value: '120-150万' },
            { value: '150万以上' },
          ]
        },
      ],
      zufang: [
        {
          title: '地区', is_show: false, list: [
            {
              value: '全部', is_selected: true
            },
            {
              value: '庆云县', is_selected: false, sub_list: [
                { value: '全部', is_selected: true },
                { value: '渤海路街道', is_selected: false },
                { value: '庆云镇', is_selected: false },
                { value: '常家镇', is_selected: false },
                { value: '尚堂镇', is_selected: false },
                { value: '崔口镇', is_selected: false },
                { value: '东辛店镇', is_selected: false },
                { value: '严务乡', is_selected: false },
                { value: '中丁乡', is_selected: false },
                { value: '徐园子乡', is_selected: false }]
            }
          ]
        },
        {
          title: '价格', is_show: false, list: [
            { value: '全部', is_selected: true },
            { value: '1000以下' },
            { value: '1000-2000' },
            { value: '2000-3000' },
            { value: '3000-5000' },
            { value: '5000以上' },
          ]
        },
      ],
      zhongjie: [
        {
          title: '地区', is_show: false, list: [
            {
              value: '全部', is_selected: true
            },
            {
              value: '庆云县', is_selected: false, sub_list: [
                { value: '全部', is_selected: true },
                { value: '渤海路街道', is_selected: false },
                { value: '庆云镇', is_selected: false },
                { value: '常家镇', is_selected: false },
                { value: '尚堂镇', is_selected: false },
                { value: '崔口镇', is_selected: false },
                { value: '东辛店镇', is_selected: false },
                { value: '严务乡', is_selected: false },
                { value: '中丁乡', is_selected: false },
                { value: '徐园子乡', is_selected: false }]
            }
          ]
        }
      ]
    }
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid,
      head_title: title[options.type],
      head_titleT: options.type
    })
    wx.setNavigationBarTitle({
      title: title[options.type],
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/getAddressFilter?token=' + this.data.token,
      success: res=>{
        var new_filter = filter[options.type];
        new_filter[0] = res.data;
        this.setData({
          filter: filter[options.type]
        })
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/House/lists?type=' + arr[options.type] + '&token=' + this.data.token,
      success: res => {
        this.setData({
          list: res.data,
          type: options.type
        })
        wx.hideLoading()
      }
    })
  },
  setFilterPanel: function(e){
    var findex = e.currentTarget.dataset.findex;
    var filter = this.data.filter;
    filter.forEach(function(value, key){
      if (key == findex){
        filter[key].is_show = !value.is_show;
      }else{
        filter[key].is_show = false;
      }
    })
    this.setData({
      filter: filter
    })
  },
  hideFilter:function(){
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      filter[key].is_show = false;
    })
    this.setData({
      filter: filter
    })
  },
  setCateIndex:function(e){
    var keys = e.currentTarget.dataset.keys;
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      if(value.is_show){
        value.list.forEach(function(vo, idx){
          if(idx == keys){
            filter[key].list[idx].is_selected = true;
          }else{
            filter[key].list[idx].is_selected = false;
          }
        })
      }
    })
    this.setData({
      filter: filter
    })
  },
  setSubcateIndex:function(e){
    console.log(e);
    var subindex = e.currentTarget.dataset.key;
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      if (value.is_show) {
        value.list.forEach(function (item, index) {
          if(item.is_selected){
            item.sub_list.forEach(function(vo, idx){
              if (idx == subindex) {
                filter[key].list[index].sub_list[idx].is_selected = true;
              } else {
                filter[key].list[index].sub_list[idx].is_selected = false;
              }
            })
          }
        })
      }
    })
    this.setData({
      filter: filter
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  backto: function () {
    wx.navigateBack({
      
    })
  }
})